from pywebio3 import start_server
from pywebio3.input import input
from pywebio3.output import put_text, put_markdown


def guess_number_game():
    import random
    target_number = random.randint(1, 100)
    attempts = 0
    while True:
        guess = input("猜一个 1 到 100 之间的数字")
        attempts += 1
        try:
            guess = int(guess)
            if guess < target_number:
                put_text("太小了，再试一次！")
            elif guess > target_number:
                put_text("太大了，再试一次！")
            else:
                put_markdown(f"答对了！你用了 {attempts} 次猜对。")
                break
        except ValueError:
            put_text("请输入一个有效的数字。")


if __name__ == "__main__":
    start_server(guess_number_game, port=8080, debug=True)